<?php
/**
 * Created by PhpStorm.
 * User: Liulibao
 * Date: 2019/10/11
 * Time: 22:05
 */

namespace app\common\model;


use think\Model;

class AfficheModel extends Model
{
    protected $table = 'ds_affiches';

    public $page_info;

    protected $pageSize = 10;

    /**
     * @description: 获取公告分类列表  @MrLiu
     * @param array $condition
     * @param $page
     * @param string $order
     * @return false|\PDOStatement|string|\think\Collection
     * @internal param bool $isArray
     */
    public function getAfficheList($condition = [], $page, $order = 'af.affiche_id desc')
    {
        $result = db('affiches')->alias('af');

        if (!empty($condition) && is_array($condition)) {
            $result = $result->where($condition);
        }

        if ($page) {
            $result = $result->field('af.*,affiche_types.name,admin.admin_name')
                ->join('affiche_types', 'af.typeid = affiche_types.id','LEFT')
                ->join('admin', 'af.opration_id = admin.admin_id','LEFT')
                ->order($order)
                ->paginate($page, false, ['query' => request()->param()]);

            $this->page_info = $result;

            return $result->items();
        } else {
            return $result->order($order)->limit(10)->select();
        }
    }


        /**
     * @description: 获取公告分类列表  @MrLiu
     * @param array $condition
     * @param $page
     * @param string $order
     * @return false|\PDOStatement|string|\think\Collection
     * @internal param bool $isArray
     */
    public function getAfficheListHome($condition = [], $page,$member_id, $order = 'af.affiche_id desc')
    {
        $result = $this->alias('af');

        if (!empty($condition) && is_array($condition)) {
            $result = $result->where($condition);
        }

        if ($page) {
            $result = $result->field('*')
                ->join('affiche_types', 'af.typeid = affiche_types.id','LEFT')
                ->join('admin', 'af.opration_id = admin.admin_id','LEFT')
                ->with(['memberid'=>function($query) use ($member_id){
                    $query->where(['member_id'=>1]);
                    }])
                ->order($order)
                ->paginate($page, false, ['query' => request()->param()]);

            $this->page_info = $result;
            
            return $result->items();
        } else {
            return $result->order($order)->limit(10)->select();
        }
    }


    public function memberid()
    {
        return $this->hasMany('AfficheReadModel','affiche_id','affiche_id');
    }

    /**
     * @description:   @MrLiu
     * @param array $condition
     * @param $page
     * @param string $field
     * @return array
     */
    public function getList($condition = [], $field = '')
    {
        $fields = $field ? $field : '*';
        $conditions = !empty($condition) ? $condition : array('affiche_id' => array('gt', '0'));
        return $this->where($conditions)
            ->field($fields)
            ->order('affiche_id desc')
            ->paginate($this->pageSize)
            ->toArray();
    }

    /**
     * @description: 获取某一条数据  @MrLiu
     * @param $id
     * @return array|bool|false|\PDOStatement|string|Model
     */
    public function getOneAfficheById($id)
    {
        if (intval($id) <= 0) {
            return false;
        }

        return db('affiches')->alias('af')->field('af.affiche_id,af.title,af.contents,affiche_types.name,af.typeid,af.start_time,af.end_time,af.sort,af.opration_id')
            ->join('affiche_types', 'af.typeid = affiche_types.id','LEFT')
            ->where(array('af.affiche_id' => $id))->find();
    }

    /**
     * @description: 根据条件查询获取某一条数据 @MrLiu
     * @param array $condition
     * @return array|bool|false|\PDOStatement|string|Model
     */
    public function getOneAffiche($condition = [])
    {
        if (empty($condition)) {
            return false;
        }

        return $this->where($condition)->find();
    }

    /**
     * @description: 新增数据  @MrLiu
     * @param $data
     * @return int|string
     */
    public function addAffiche($data)
    {
        $data['created_at'] = time();
        return $this->insertGetId($data);
    }

    /**
     * @description: 更新信息  @MrLiu
     * @param $data
     * @param $ac_id
     * @return $this
     */
    public function editAffiche($data,$id)
    {
        return $this->where('affiche_id',$id)->update($data);
    }

    /**
     * @description: 删除分类  @MrLiu
     * @param $id
     * @return int
     */
    public function delAffiche($id)
    {
        return $this->where('affiche_id', $id)->delete();
    }

    /**
     * @description: 统计公告最新条数  @MrLiu
     */
    public function counts()
    {
        return $this->count();
    }
}